package com.google.code.customercare.common;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport implements SessionAware, ApplicationAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Result result;
	private Map<String, Object> application;
	private Map<String, Object> session;
	
	
	
	// Dung de phan trang
	protected int hasSearch;
	protected String pageIndex;
	protected String pageSize;
	protected String totalPage;
	
	protected String token;
	
	private static Logger log = Logger.getLogger(BaseAction.class);
	
	public BaseAction() {
		result = new Result();
		result.setMsg("");
		// Mac dinh: Khong biet di dau thi ve nha
		result.setDestination("");
		// Mac dinh: Luon reload lai trang
		result.setReloadPage("true");
	}
	
	public Map<String, Object> getApplication() {
		return application;
	}

	public void setApplication(Map<String, Object> value) {
		application = value;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> value) {
		session = value;
	}
	
	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}
	
	protected void setMessage(String msg) {
		// Encode de dam bao font tieng viet khong bi vo khi thuc hien redirect message nay den action khac
		// truoc khi tra ve jsp
		try {
			result.setMsg(URLEncoder.encode(msg, Constants.URL_ENCODER));
		} catch (UnsupportedEncodingException e) {
			log.error("Exception", e);
		}
	}
	
	protected void setDestination(String dest) {
		result.setDestination(dest);
	}
	
	protected void setReloadPage(String reloadPage) {
		result.setReloadPage(reloadPage);
	}

	public String getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(String pageIndex) {
		this.pageIndex = pageIndex;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(String totalPage) {
		this.totalPage = totalPage;
	}

	public String getToken() {
		return token;
	}
	public void setToken(String token) {
		this.token = token;
	}
	
	protected String genToken() {
		long now = System.currentTimeMillis();
		return now + "" + RandomStringUtils.randomNumeric(Constants.TOKEN_LENGTH);
	}
	
	public int getHasSearch() {
		return hasSearch;
	}

	public void setHasSearch(int hasSearch) {
		this.hasSearch = hasSearch;
	}
	
	protected abstract boolean checkToken();
	protected abstract void rememberToken();
}
